package com.zzyl.service.impl;



import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.dto.NursingProjectPlanDto;
import com.zzyl.entity.NursingPlan;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.mapper.NursingprojectplanMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.vo.NursingPlanVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class NursingPlanServiceimpl implements NursingPlanService {
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private NursingprojectplanMapper nursingprojectplanMapper;


    /**
     * 查询所有护理计划
     *
     * @return
     */

    @Override
    public List<NursingPlanVo> listAll() {
        List<NursingPlanVo> list = nursingPlanMapper.listAll();
        list.stream().map(nursingPlanVo -> {
            NursingPlanVo nursingPlanVo1 = new NursingPlanVo();
            BeanUtils.copyProperties(nursingPlanVo, nursingPlanVo1);
            return nursingPlanVo1;
        }).collect(Collectors.toList());
        return list;
    }

    /**
     * 新增护理计划
     *
     * @param nursingPlanDto
     */
    @Override
    public void add(NursingPlanDto nursingPlanDto) {
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDto, nursingPlan);
        nursingPlan.setCreateTime(LocalDateTime.now());
        nursingPlan.setCreateBy(1671403256519078153l);
        nursingPlan.setStatus(0);
        nursingPlan.setSortNo(1);
        nursingPlanMapper.add(nursingPlan);

        List<NursingProjectPlanDto> nursingProjectPlanVoList = nursingPlanDto.getProjectPlans();

        nursingProjectPlanVoList.forEach(nppl -> {
            nppl.setPlanId(nursingPlan.getId());
        });
        nursingprojectplanMapper.addlist(nursingProjectPlanVoList);

        /*ArrayList<NursingProjectPlan> projectPlanList = new ArrayList<>();
        nursingProjectPlanVoList.forEach(nppl->{
            NursingProjectPlan nursingProjectPlan = BeanUtil.toBean(nppl, NursingProjectPlan.class);
            nursingProjectPlan.setPlanId(nursingPlan.getId());
            projectPlanList.add(nursingProjectPlan);
        });
        nursingprojectplanMapper.addlist(nursingPlanDto.getProjectPlans());*/

    }

    /**
     * 分页查询护理计划
     *
     * @param pageNum
     * @param pageSize
     * @param name
     * @param status
     * @return
     */
    @Override
    public PageResponse<NursingPlanVo> PageNursing(Integer pageNum, Integer pageSize, String name, Integer status) {

        PageHelper.startPage(pageNum, pageSize);
        Page<NursingPlan> page = nursingPlanMapper.PageNursing(name, status);
        return PageResponse.of(page, NursingPlanVo.class);
    }

    /**
     * 根据id查询护理计划
     *
     * @param id
     * @return
     */
    @Override
    public NursingPlanVo getNursing(Long id) {
        return nursingPlanMapper.getNursing(id);



    }

    /**
     * 修改护理计划
     *
     * @param nursingPlanDto
     */
    @Override
    public void update(NursingPlanDto nursingPlanDto) {
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDto, nursingPlan);
        nursingPlan.setUpdateTime(LocalDateTime.now());
        nursingPlan.setUpdateBy(1l);
        nursingPlanMapper.update(nursingPlan);

        nursingprojectplanMapper.delete(nursingPlanDto.getId());
        List<NursingProjectPlanDto> nursingProjectPlanVoList = nursingPlanDto.getProjectPlans();

        nursingProjectPlanVoList.forEach(nppl -> {
            nppl.setPlanId(nursingPlan.getId());
        });
        nursingprojectplanMapper.addlist(nursingProjectPlanVoList);


    }


    /**
     * 删除护理计划
     *
     * @param id
     */
    @Override
    public void delete(Long id) {
        nursingPlanMapper.delete(id);
        nursingprojectplanMapper.delete(id);
    }


    /**
     * 修改护理计划状态
     *
     * @param id
     * @param status
     */
    @Override
    public void modifyState(Long id, Integer status) {
        nursingPlanMapper.modifyState(id, status);
    }


}
